package com.study.www.demo2filter.config;

import com.study.www.demo2filter.Application;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.web.filter.AbstractRequestLoggingFilter;

import javax.annotation.PostConstruct;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhuyanpeng
 * @since 2020/1/11
 */
@Slf4j
@WebFilter(urlPatterns = {"/demo2/*"})
public class Demo4Filter extends AbstractRequestLoggingFilter {

    @PostConstruct
    void init(){
        this.setIncludeQueryString(true);
        this.setBeforeMessagePrefix("我是消息前缀setBeforeMessagePrefix");
        this.setAfterMessagePrefix("我是消息后缀setAfterMessagePrefix");
        this.setBeforeMessageSuffix("我是消息前缀setBeforeMessageSuffix");
        this.setAfterMessageSuffix("我是消息后缀setAfterMessageSuffix");
    }

    @Override
    protected void beforeRequest(HttpServletRequest request, String message) {
        log.warn("beforeRequest 信息=>"+message);
        log.warn("Demo4Filter 的 beforeRequest;进入=>"+new DateTime().toString(Application.DATEFORMAT));
    }

    @Override
    protected void afterRequest(HttpServletRequest request, String message) {
        log.warn("Demo4Filter 的 afterRequest;进入=>"+new DateTime().toString(Application.DATEFORMAT));
        log.warn("afterRequest 信息=>"+message);
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        filterChain.doFilter(request,response);
    }
}
